//
//  KLBasketViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLNearByViewController.h"
#import "KLBasketTableViewCell.h"
#import "KLShopModel.h"
#import "KLShopDetailViewController.h"
@interface KLNearByViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) STNoresultView                     *noResultView;
@end

@implementation KLNearByViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"附近商家";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNearByRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    //无数据vieww
    __weak typeof(self) weakSelf =  self;
    self.noResultView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 350)
                                                        title:@"暂无商户"
                                                  buttonTitle:@""
                                                 buttonHandle:^(NSString *titleString) {
                                                    
                                                 }];
    self.noResultView.hidden = YES;
    [self.tableView addSubview:self.noResultView];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLBasketTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLBasketTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLBasketTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.shopModel = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KLShopModel  * model = self.dataSouce[indexPath.row];
    KLShopDetailViewController * vc = [KLShopDetailViewController new];
    vc.shops_ID = model.shops_ID;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --NetWork Method
- (void)loadNearByRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/shopslist"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 self.dataSouce = [NSMutableArray new];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * list = dataDic[@"list"];
                                                     for (NSDictionary * listdic in list) {
                                                         KLShopModel * model = [KLShopModel  mj_objectWithKeyValues:listdic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 self.noResultView.hidden = self.dataSouce.count;
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                  self.noResultView.hidden = self.dataSouce.count;
                                                 // [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             }];
}
@end

